import {
  LOGIN,
  ROUTES,
  REGISTER
} from '@/services/api'
import {request, METHOD, removeAuthorization} from '@/utils/request'

/** 
 * 登录服务
 * @param email 邮箱
 * @param password 账户密码
 * @param type 登录方式，101：邮箱登录
 * @param code 验证码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(email, password, type, code) {
  return request(LOGIN, METHOD.POST, {
    email,
    password,
    type,
    code
  })
}


/**
 * 获取动态路由
 * @param appId 项目id 
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getRoutesConfig(appId) {
  return request(ROUTES, METHOD.GET, {
    appId
  })
}


/**
 * 注册
 * @param nickName 账户名
 * @param email 邮箱
 * @param password 账户密码
 * @param code 验证码
 * @returns {Promise<AxiosResponse<T>>}
 */
 export async function register(email, password, code, nickName) {
  return request(REGISTER, METHOD.GET, {
    email, password, code, nickName
  })
}


/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization()
}
export default {
  login,
  logout,
  getRoutesConfig,
  register
}
